import time

import pymysql
class SocialSecurity:
    # 公司名称
    company_name = ''
    update_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
    invalid_time = int(time.mktime(time.strptime(update_time, "%Y-%m-%d %H:%M:%S"))) + 864000
    # 文件是否存system(system|normal)
    file_type = 'system'
    # 文件路径
    file_path = ''
    # 记录类型
    record_type = ''
    # 参保人名
    people_name = ''
    # 身份证
    id_number = ''
    # sql模板
    sql = 'insert into record_file()'

    updata_sql = 'update record_file set status = \'1\''

    # 添加初始值
    def __init__(self):
        pass

    # 通用存储路径
    def general(self, company_name, file_path, record_type, task_type):
        self.file_path = file_path.replace('\\', '/')
        self.record_type = record_type.split('-')[0]
        self.sql = 'insert into record_file(company_name,file_path,record_type,status,update_time) ' \
                   'values(\'{}\',\'{}\',\'{}\',\'{}\',\'{}\')'.format(company_name, self.file_path,
                                                                       self.record_type, task_type, self.update_time)
        # print(self.sql)
        self.updata_sql = 'update record_file set status = \'1\' where company_name=\'{}\' ' \
                          'and record_type=\'{}\''.format(company_name,self.record_type)
        sql_updata(self.updata_sql)
        sql_add(self.sql)

    # 个人参保证明文件路径存放数据库
    def personal_certificate(self, company_name, file_path, record_type, id_number, file_name, people_name):
        self.file_path = file_path.replace('\\', '/')
        self.sql = 'insert into record_file(company_name,file_path,record_type,people_name,id_number,' \
                   'file_name,invalid_time,status,update_time) ' \
                   'values(\'{}\',\'{}\',\'{}\',\'{}\',\'{}\',\'{}\',\'{}\',\'{}\',\'{}\')'.format(
                    company_name, self.file_path, record_type, people_name, id_number, file_name,
                    str(self.invalid_time), '0', self.update_time)
        self.updata_sql = 'update record_file set status = \'1\' where company_name=\'{}\' ' \
                          'and record_type=\'{}\''.format(company_name, self.record_type)
        sql_updata(self.updata_sql)
        sql_add(self.sql)


def run_write(company_name, file_path, record_type, task_type, **kwargs):

    if task_type == '1':
        # 功能逻辑问题，不会处理事件
        socialsecurity = SocialSecurity()
        socialsecurity.general(company_name, file_path, record_type, task_type)
    else:
        print(record_type)
        fixed_value = ['个人参保证明', '人员信息查询', '单位参保证明', '单位信息查询', '单位征收计划查询','已通过信息','未通过信息']
        # 个人参保证明
        if record_type == fixed_value[0]:
            socialsecurity = SocialSecurity()
            socialsecurity.personal_certificate(company_name, file_path, record_type,
                                                kwargs['id_number'], kwargs['file_name'], kwargs['people_name'])
        # 人员信息查询
        elif record_type == fixed_value[1]:
            pass
            # socialsecurity = SocialSecurity()
            #
            # 待定
            #
            #
            #
        # 单位参保证明
        elif record_type == fixed_value[2]:
            socialsecurity = SocialSecurity()
            socialsecurity.general(company_name, file_path, record_type, task_type)
        # 单位信息查询
        elif record_type == fixed_value[3]:
            socialsecurity = SocialSecurity()
            socialsecurity.general(company_name, file_path, record_type, task_type)
        # 单位征收计划查询
        elif record_type == fixed_value[4]:
            socialsecurity = SocialSecurity()
            socialsecurity.general(company_name, file_path, record_type, task_type)
        # 已通过信息
        elif record_type == fixed_value[5]:
            socialsecurity = SocialSecurity()
            socialsecurity.general(company_name, file_path, record_type, task_type)
        # 未通过信息
        elif record_type == fixed_value[6]:
            socialsecurity = SocialSecurity()
            socialsecurity.general(company_name, file_path, record_type, task_type)
        else:
            ts = time.strftime("%Y-%m-%d %H:%M", time.localtime())
            f = open('conf/social_security/synchronize/log.txt', 'a+')
            f.write('文件未识别：' + '\t' + file_path + '\t' + ts + '\n')
            print('未识别')
            return False

def sql_add(sql_text):
    conn = pymysql.connect(host="127.0.0.1", user="root", database="social_security", password="753951", charset="utf8")
    cursor = conn.cursor()
    cursor.execute(sql_text)
    conn.commit()
    cursor.close()
    conn.close()

def sql_updata(sql_text):
    conn = pymysql.connect(host="127.0.0.1", user="root", database="social_security", password="753951", charset="utf8")
    cursor = conn.cursor()
    cursor.execute(sql_text)
    conn.commit()
    cursor.close()
    conn.close()
